home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro22 / lssearch.c < prev    next >
Text File  |  1990-06-20  |  4KB  |  134 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lssearch.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* non-ansi headers */
  12. #include <bool.h>
  13.  
  14. /* library headers */
  15. #include <blkio.h>
  16.  
  17. /* local headers */
  18. #include "lseq_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      lssearch - lseq search
  23.  
  24. SYNOPSIS
  25.      #include <lseq.h>
  26.  
  27.      int lssearch(lsp, offset, buf, bufsize, cmp)
  28.      lseq_t *lsp;
  29.      size_t offset;
  30.      const void *buf;
  31.      size_t bufsize;
  32.      int (*cmp)(const void *p1, const void *p2, size_t n);
  33.  
  34. DESCRIPTION
  35.      The lssearch function performs a linear search through lseq lsp
  36.      for a record with a field matching the field pointed to by buf.
  37.      The field being searched begins offset characters from the
  38.      beginning of the record and is bufsize characters long.  The
  39.      function pointed to by cmp is used to test for a match.  The
  40.      search begins on the record following the current record, so
  41.      before the first call to lssearch the cursor should be set to
  42.      null, and successive calls will find succesive records with
  43.      fields matching buf.  If the field is matched, the cursor is left
  44.      on the record with the matching field.
  45.  
  46.      The user supplied comparison function cmp must be of the
  47.      following form:
  48.  
  49.           int cmp(const void *p1, const void *p2, size_t n);
  50.  
  51.      where p1 and p2 point to the two keys to bey compared and n is
  52.      the key size.  The return must be less than, equal to, or greater
  53.      than zero if p1 is less than, equal to, or greater than p2,
  54.      respectively.  If cmp is NULL then the memcmp function is used as
  55.      the default.
  56.  
  57.      lssearch will fail if one or more of the following is true:
  58.  
  59.      [EINVAL]       lsp is not a valid lseq pointer.
  60.      [EINVAL]       buf is the NULL pointer.
  61.      [EINVAL]       bufsize is less than 1.
  62.      [LSEBOUND]     offset + bufsize extends beyond the
  63.                     end of the record.
  64.      [LSELOCK]      lsp is not read locked.
  65.      [LSENOPEN]     lsp is not open.
  66.  
  67. SEE ALSO
  68.      lscursor.
  69.  
  70. DIAGNOSTICS
  71.      Upon successful completion, a value of 1 is returned if the field
  72.      was matched or a value of 0 if it was not.  Otherwise, a value of
  73.      -1 is returned, and errno set to indicate the error.
  74.  
  75. ------------------------------------------------------------------------------*/
  76. int lssearch(lsp, offset, buf, bufsize, cmp)
  77. lseq_t *lsp;
  78. size_t offset;
  79. const void *buf;
  80. size_t bufsize;
  81. lscmp_t cmp;
  82. {
  83.     bool found = FALSE;
  84.  
  85.     /* validate arguments */
  86.     if (!ls_valid(lsp) || buf == NULL || bufsize < 1) {
  87.         errno = EINVAL;
  88.         return -1;
  89.     }
  90.  
  91.     /* check if not open */
  92.     if (!(lsp->flags & LSOPEN)) {
  93.         errno = LSENOPEN;
  94.         return -1;
  95.     }
  96.  
  97.     /* check if not read locked */
  98.     if (!(lsp->flags & LSRDLCK)) {
  99.         errno = LSELOCK;
  100.         return -1;
  101.     }
  102.  
  103.     /* check if over record boundary */
  104.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  105.         errno = LSEBOUND;
  106.         return -1;
  107.     }
  108.  
  109.     /* check if cmp is NULL */
  110.     if (cmp == NULL) {
  111.         cmp = memcmp;
  112.     }
  113.  
  114.     /* advance cursor one record */
  115.     if (lsnext(lsp) == -1) {
  116.         LSEPRINT;
  117.         return -1;
  118.     }
  119.  
  120.     while (lsp->clspos != NIL) {
  121.         if ((*cmp)(((char *)lsp->clsrp->recbuf + offset), buf, bufsize) == 0) {
  122.             found = TRUE;
  123.             break;
  124.         }
  125.         if (lsnext(lsp) == -1) {
  126.             LSEPRINT;
  127.             return -1;
  128.         }
  129.     }
  130.  
  131.     errno = 0;
  132.     return (found ? 1 : 0);
  133. }
  134.